/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.folha.dao;

import br.com.folha.utilidades.HibernateUtil;
import java.io.Serializable;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
/**
 *
 * @author marceloml
 */
public class GenericoDao implements IGenericoDAO,Serializable{
    
    private static final long serialVersionUID = 1L;

    @Override
    public boolean salvar(Object t) {        
        Session sessao = HibernateUtil.getSessionFactory();
        Transaction transacao = sessao.beginTransaction();
        try {
            sessao.save(t);
            transacao.commit();
        } catch (HibernateException erro) {
            transacao.rollback();
            return false;
        } finally {
            sessao.close();
        }
        return true;
    }

    @Override
    public boolean excluir(Object t) {
        Session sessao = HibernateUtil.getSessionFactory();
        Transaction transacao = sessao.beginTransaction();        
        try {
            sessao.delete(t);
            transacao.commit();

        } catch (HibernateException erro) {
            transacao.rollback();
            return false;
        } finally {
            sessao.close();
        }
        return true;        
    }

    @Override
    public boolean atualizar(Object t) {
        Session sessao = HibernateUtil.getSessionFactory();
        Transaction transacao = sessao.beginTransaction();

        try {
            sessao.update(t);
            transacao.commit();

        } catch (HibernateException erro) {
            transacao.rollback();
            return false;
        } finally {
            sessao.close();
        }
        return true;        
    }

    @Override
    public List<Object> listar(Object t) {             
        
        Session sessao = HibernateUtil.getSessionFactory();
        Criteria criteria = sessao.createCriteria(t.getClass());                
        List lista = criteria.list();
        sessao.close();        
        return lista;        
    }           
    
}